//
//  CommonFunctions.m
//  猎鹰金融
//
//  Created by Danny on 16/8/5.
//  Copyright © 2016年 肖棟棟. All rights reserved.
//

#import "CommonFunctions.h"
static CommonFunctions *sharedInstance = nil;
@implementation CommonFunctions

+(instancetype)sharedInstance{
    return [[CommonFunctions alloc]init];
}


+(UIImage *)squareImageFromImage:(UIImage *)image scaledToSize:(CGFloat)newSize{
    CGAffineTransform scaleTransform;
    CGPoint origin;
    
    if (image.size.width > image.size.height) {
        //image的原始高度为200 缩放image的高度为400pixels
        CGFloat scaleRatio = newSize/image.size.height;
        scaleTransform = CGAffineTransformMakeScale(scaleRatio, scaleRatio);
        //设置绘制原始图片的画笔坐标为CGPoint(-100,0)pixels
        origin = CGPointMake(-(image.size.height - image.size.width)/2.0, 0);
    }else{
        //image的高度为400 宽度为200
        CGFloat scaleRatio = newSize/image.size.width;
        scaleTransform = CGAffineTransformMakeScale(scaleRatio, scaleRatio);
        origin = CGPointMake(0, -(image.size.height - image.size.width)/2.0);
    }
    CGSize size = CGSizeMake(newSize, newSize);
    
    //创建画板为400*400像素
    if ([[UIScreen mainScreen]respondsToSelector:@selector(scale)]) {
        UIGraphicsBeginImageContextWithOptions(size, YES, 0);
    }else{
        UIGraphicsBeginImageContext(size);
    }
    CGContextRef context = UIGraphicsGetCurrentContext();
    //将原始图片(400*200)pixels缩放为(800 *400)像素
    CGContextConcatCTM(context, scaleTransform);
    //origin也会从原始(-100,0)缩放到(-200,0)
    [image drawAtPoint:origin];
    //获取缩放后剪切的image图片
    image =UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

@end
